
layui.define(['admin', 'table', 'util','form','element','upload'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form, upload = layui.upload;
    var tableIn = table.render({
        elem: "#list",
        url: "/shengteng/Shop/swiperList",
        toolbar: "#toolbar",
        defaultToolbar: [],
        title: "轮播列表",
        cols: [[
            {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true},
            {field: 'img_url', title: '轮播图', width: 200, toolbar: "#swiper"},
            {field: 'sort_num', title: '排序值', width: 80},
            {field: 'status', title: '显示', width: 100, toolbar: '#status'},
            {field: 'create_time', title: '添加时间', width: 200},
            {field: 'update_time', title: '修改时间', width: 200},
            {fixed: 'right', title: '操作', toolbar: '#action', width: 150}
        ]],
        page: true
    })
    //添加轮播图
    var events = {
        add: function(){
            var index = layer.open({
                type: 2,
                content: '/shengteng/Shop/addSwiper',
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index)
        },
    };
    $('.clt-auth-btns .layui-btn').on('click', function(){
        var othis = $(this),thisEvent = othis.data('events');
        events[thisEvent] && events[thisEvent].call(this);
    });
    //添加提交
    form.on('submit(add)', function(data){
        admin.req({
            url: '/shengteng/Shop/addSwiper'
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });
    //修改状态
    form.on('switch()', function(obj){
        loading =layer.load(1, {shade: [0.1,'#fff']});
        var param={};
        param[obj.elem.name] = obj.elem.checked===true?1:0;
        param['id'] = this.value;
        admin.req({
            url: '/shengteng/Shop/cfSwiper',
            data: param,
            type:'post',
            success: function (res) {
                if(res.code>0){
                    if(obj.elem.checked==true){
                        $(obj.elem).prop('checked',false);
                    }else{
                        $(obj.elem).prop('checked',true);
                    }
                    form.render();
                }else{
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                layer.closeAll('loading');
            }
        });
    });


    //监听工具条
    table.on('tool(list)', function(obj){
        var data = obj.data;
        var id = data.id;
        if(obj.event === 'del_btn'){
            layer.confirm('您确定要删除该轮播图吗？', function(index){
                admin.req({
                    url: '/shengteng/Shop/delSwiper/'
                    , type: 'post'
                    , data: {'id':id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if(res.code==0){
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        } else if(obj.event === 'edit'){
            var index = layer.open({
                type: 2,
                content: '/shengteng/Shop/editSwiper/id/'+id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //提交编辑
    form.on('submit(edit)', function(data){
        admin.req({
            url: '/shengteng/Shop/editSwiper'
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });
    //展示图上传
    var uploadInst = upload.render({
        elem: '#image'
        , url: '/shengteng/Upfiles/upimage' //改成您自己的上传接口
        , before: function (obj) {
            element.progress('image', '0%'); //进度条复位
            layer.msg('上传中', {icon: 16, time: 0});
        }
        , done: function (res) {
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功的一些操作
            $("#image_val").val(res.url);
            $('#image1').attr('src', res.url);
            $("#image_val").parent().append('<input type="hidden" name="image_aid" value="' + res.aid + '">')
            $('#imageText').html(''); //置空上传失败的状态
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#imageText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
        //进度条
        , progress: function (n, index, e) {
            element.progress('image', n + '%'); //可配合 layui 进度条元素使用
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    exports('swipers', {});
});
